package com.mes.device.server.impl;

import com.mes.common.pojo.MESResult;
import com.mes.common.util.Constant;
import com.mes.device.mapper.DeviceTypeMapper;
import com.mes.device.server.DeviceTypeServer;
import com.mes.pojo.device.DeviceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 张喆
 * @Company
 * @Description
 * @Time 2024/10/16 17:01
 */
@Service
public class DeviceTypeServerImpl implements DeviceTypeServer {
    @Autowired
    private DeviceTypeMapper deviceMapper;

    @Override
    public MESResult queryAll(DeviceType deviceType) {
        List<DeviceType> deviceTypes = deviceMapper.selectAll(deviceType);
        for (DeviceType devType : deviceTypes) {
            devType.setHasChildren(devType.getDeviceTypeParent());
        }
        return MESResult.success(deviceTypes);
    }

    @Override
    public MESResult getAllSelectionOptions() {
        return MESResult.success(deviceMapper.selectAllType());
    }

    @Override
    public MESResult queryChildrenByParentId(DeviceType deviceType) {
        List<DeviceType> deviceTypes = deviceMapper.selectByDeviceParentId(deviceType);
        for (DeviceType devType : deviceTypes) {
            devType.setHasChildren(devType.getDeviceTypeParent());
        }
        return MESResult.success(deviceTypes);
    }

    @Override
    public MESResult queryByDeviceTypeId(String deviceTypeId) {
        return MESResult.success(deviceMapper.selectByDeviceTypeId(deviceTypeId));
    }

    @Override
    public MESResult updateDeviceType(DeviceType deviceType) {
        DeviceType same = deviceMapper.selectByDeviceTypeName(deviceType.getDeviceTypeName());
        if (same != null && !same.getDeviceTypeId().equals(deviceType.getDeviceTypeId())){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT,"重复名字");
        }
        //如果当前创建的为子类
        if (!deviceType.getDeviceTypeParentId().equals("F")){
            DeviceType deviceTypeFather = deviceMapper.selectByDeviceTypeId(deviceType.getDeviceTypeParentId());
            //判断父类是否已经存在父类标记，不存在便跟新父状态
            if (deviceTypeFather.getDeviceTypeParent() == 1){
                deviceTypeFather.setDeviceTypeParent(0);
                deviceMapper.updateDeviceType(deviceTypeFather);
            }
        }
        return MESResult.success(deviceMapper.updateDeviceType(deviceType));
    }

    @Override
    public MESResult deleteDeviceType(String deviceTypeId) {
        DeviceType deviceType = deviceMapper.selectByDeviceTypeId(deviceTypeId);
        DeviceType tempDeviceTYpe = new DeviceType();
        tempDeviceTYpe.setDeviceTypeParentId(deviceType.getDeviceTypeId());
        List<DeviceType> deviceTypes = deviceMapper.selectByDeviceParentId(tempDeviceTYpe);
        if (deviceTypes.size() > 0){
            return MESResult.error(Constant.RESPONSE_STATUS_ERROR,"存在子类无法删除");
        }
        deviceType.setDeviceTypeStatus(99);
        return MESResult.success(deviceMapper.updateDeviceType(deviceType));
    }

    @Override
    public MESResult addNewDeviceType(DeviceType deviceType) {
        //判断是否缺少关键元素
        if (deviceType.equals(null) || deviceType.equals("")) {
            return MESResult.error("添加缺少关键元素");
        }
        //判断是否当前添加的新设备类型名是否存在
        if (deviceMapper.selectByDeviceTypeName(deviceType.getDeviceTypeName()) != null){
            return MESResult.error("当前设备类型已经存在，无法进行添加");
        }
        //生成id
        //获取当前最大的id
        String maxId = deviceMapper.selectMaxId();
        //获取当前最大id的数字编号
        String[] ids = maxId.split("_");
        int maxNum = Integer.parseInt(ids[1]);
        maxNum += 1;
        String tempNum = null;
        if (maxNum < 100 && maxNum >= 10) {
            tempNum = "0" + maxNum;
        } else if (maxNum < 10 && maxNum > 0) {
            tempNum = "00" + maxNum;
        }else {
            tempNum = maxId + "";
        }
        String newId = ids[0] + "_" + tempNum;
        deviceType.setDeviceTypeId(newId);
        //如果当前创建的为子类
        if (!deviceType.getDeviceTypeParentId().equals("F")){
            DeviceType deviceTypeFather = deviceMapper.selectByDeviceTypeId(deviceType.getDeviceTypeParentId());
            //判断父类是否已经存在父类标记，不存在便跟新父状态
            if (deviceTypeFather.getDeviceTypeParent() == 1){
                deviceTypeFather.setDeviceTypeParent(0);
                deviceMapper.updateDeviceType(deviceTypeFather);
            }
        }
        //存入数据
        return MESResult.success(deviceMapper.insertDeviceType(deviceType));
    }


}
